
// Copyright (c) 2012, 2013 All Right Reserved, http://hpc.utp.edu.my
// Author: Nordin Zakaria nordinzakaria@gmail.com
// All rights reserved.
#include "Job.h"

void Job::copy(const Job& orig)
 {
  if(&orig == this) return;

  initM = orig.initM;
  id = orig.id;

  unsigned int sz = orig.properties_size();
  properties_size(sz); 
  for (unsigned int i=0; i<sz; i++) {
	set(orig.get(i), i);	
	}

  precedence.clear();
  for (unsigned int i=0; i<orig.precedence.size(); i++) {
	precedence.push_back(orig.precedence[i]);
	}
 }

void Job::read(ifstream &infile)
	{
          unsigned int jid;
	  infile >> jid;
          setID(jid);

          unsigned int numprops;
	  infile >> numprops;
	  properties_size(numprops);
          for (unsigned int i=0; i<numprops; i++) {
                unsigned int prop;
		infile >> prop;
		set(prop, i);
		}

          unsigned int numpreced;
	  infile >> numpreced;
          for (unsigned int i=0; i<numpreced; i++) {
                unsigned int dep;
		infile >> dep;
                addPrecedence(dep);
                }
	}

void Job::read(FILE *infile, bool ascii)
	{
          unsigned int jid, numpreced;
	  if (ascii) {
		fscanf(infile, "%u", &jid);

		unsigned int numprop;
		fscanf(infile, "%u", &numprop);
	  	properties_size(numprop);
		for (unsigned int i=0; i<numprop; i++) {
		     unsigned int prop;
		     fscanf(infile, "%u", &prop);
		     set(prop, i);
		     }

		fscanf(infile, "%u", &numpreced);
		}
	  else {
	  	fread(&jid,sizeof(unsigned int),1,infile);

		unsigned int numprop;
	  	fread(&numprop,sizeof(unsigned int),1,infile);
	  	properties_size(numprop);
		for (unsigned int i=0; i<numprop; i++) {
		     unsigned int prop;
	  	     fread(&prop,sizeof(unsigned int),1,infile);
		     set(prop, i);
		     }

	  	fread(&numpreced,sizeof(unsigned int),1,infile);
		}

          setID(jid);

          for (unsigned int i=0; i<numpreced; i++) {
                unsigned int dep;
		if (ascii)
		    fscanf(infile, "%u", &dep);
		else
		    fread(&dep,sizeof(unsigned int),1,infile);
                addPrecedence(dep);
                }
	}

void Job::write(FILE *outfile)
	{
     	fwrite(&id, sizeof(unsigned int), 1, outfile);

	unsigned int numprop = properties.size();
     	fwrite(&numprop, sizeof(unsigned int), 1, outfile);
	for (unsigned int i=0; i<numprop; i++) {
		unsigned int prop = get(i);
     		fwrite(&prop, sizeof(unsigned int), 1, outfile);
		}
	
	unsigned int psize = precedence.size();
     	fwrite(&psize, sizeof(unsigned int), 1, outfile);
     	for (unsigned int i=0; i<precedence.size(); i++)
       		{
		 unsigned int pred = precedence[i];
		 fwrite(&pred, sizeof(unsigned int), 1, outfile);
       		}
	}

ostream& operator<<(ostream& os, const Job& obj)
      {
    	if (os == cout) 
	{
    	unsigned int numprop =  obj.properties.size();
	cout << "Job id: " << obj.id << ", numprop= " << numprop << endl << "\t props: ";
    	for (unsigned int i=0; i<numprop; i++)
		{
    		unsigned int prop = obj.get(i);
		cout << prop << " ";
		}
	cout << endl << "\t precedence: ";
        for (unsigned int i=0; i<obj.precedence.size(); i++) {
		cout << obj.precedence[i] << " ";
		}
	cout << endl;
	return os;
	}

  	os.write ((char*)&obj.id, sizeof (unsigned int));

	unsigned int numprop = obj.properties.size();
  	os.write ((char*)&numprop, sizeof (unsigned int));
	for (unsigned int i=0; i<numprop; i++) {
		unsigned int prop = obj.get(i);
  		os.write ((char*)&prop, sizeof (unsigned int));
		}
	
	unsigned int psize = obj.precedence.size();
	os.write ((char*)&psize, sizeof (unsigned int));
        for (unsigned int i=0; i<obj.precedence.size(); i++) {
	  	os.write ((char*)&obj.precedence[i], sizeof (unsigned int));
        	}
      	return os;
      }

istream& operator>>(istream& is, Job& job)
        {
        // read obj from stream
	char buffer[128];
        unsigned int jid;
	is.read(buffer, sizeof(unsigned int));
	jid = atoi(buffer);
        job.setID(jid);


        unsigned int numprop;
	is.read(buffer, sizeof(unsigned int));
	numprop = atoi(buffer);
  	job.properties_size(numprop);
	for (unsigned int i=0; i<numprop; i++) {
        	unsigned int prop;
		is.read(buffer, sizeof(unsigned int));
		prop = atoi(buffer);
		job.set(prop, i);
		}

        unsigned int numpreced;
	is.read(buffer, sizeof(unsigned int));
	numpreced = atoi(buffer);

        for (unsigned int i=0; i<numpreced; i++) {
                unsigned int dep;
		is.read(buffer, sizeof(unsigned int));
	  	dep = atoi(buffer);
		job.addPrecedence(dep);
                }

          return is;
        }


